package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Constants;
import util.DBConnection;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fullname = request.getParameter("fullname");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String stream = request.getParameter("stream");
        String year = request.getParameter("year");
        String roll = request.getParameter("roll");
        String subject = request.getParameter("subject");
        String type = request.getParameter("user_type");
        HttpSession session = request.getSession();
        session.setAttribute("reg", "Error");
        String redirectUrl = "/Register.jsp";
        try {
            Connection con = DBConnection.getConn();
            if (con != null) {
                con.setAutoCommit(false);
                Statement statement = con.createStatement();
                String query = "select * from login_details where USER_NAME='" + username + "'";
                ResultSet resultSet = statement.executeQuery(query);
                if (resultSet.next()) {
                    session.setAttribute("reg", username);
                } else {
                    query = "insert into login_details values (USER_SEQ.NEXTVAL,'" + username + "','" + password + "','" + type + "','" + fullname + "')";
                    statement.execute(query);
                    if (Constants.Student.equalsIgnoreCase(type)) {
                        query = "insert into STUDENT values (USER_SEQ.CURRVAL,'" + stream + "','" + year + "'," + roll + ")";
                        statement.execute(query);
                    } else {
                        query = "insert into FACULTY values (USER_SEQ.CURRVAL,'" + subject + "')";
                        statement.execute(query);
                    }
                    redirectUrl = "/home.jsp";
                    session.setAttribute("reg", fullname);
                }
                resultSet.close();
                statement.execute("COMMIT");
                statement.close();
            con.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + redirectUrl);
    }
}
